This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.

Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter.

1. Which census tract has the highest percentage of your target race/ethnicity and what is the percent? Which has the highest median household income and how much is it?

Census Tract with highest percentage: 24510200702 Census Tract 2007.02, Baltimore city, Maryland

Percentage: 99.32%

Highest Median Household Income: 24510271102 Census Tract 2711.02, Baltimore city, Maryland

Highest Median Income: $195,156

race_ethn <- get_acs(geography = "tract", 
     variables = c("total_race" = "B03002_001", #Total Population data
                 "not_horl" = "B03002_002", #Not Hispanic or Latine
                 "BlAfm" = "B03002_004" #Black or African American alone
                   ),
     year = 2019,
     survey = "acs5",
     state = c(24), #Maryland
     county = c(510), #Baltimore City and PG County
     geometry = TRUE, # download the shapefile with the data
     output = "wide") # need this
## Getting data from the 2015-2019 5-year ACS
med_income <- get_acs(geography = "tract",
                      variables = c("income" = "B19013_001" #median household income
                      ),
                      year = 2019,
                      survey = "acs5",
                      state = c(24),
                      county = c(510),
                      geometry = TRUE,
                      output = "wide")
## Getting data from the 2015-2019 5-year ACS

Which census tract has the highest percentage of your target race/ethnicity? What is the percent?

#First divide the "BlAfm" population totals by the total race and multiply by 100 to get the percent of population in each tract that is Black and African American alone that is not Hispanic or Latine
race_ethn$BlAfm_pop <- ((race_ethn$BlAfmE / race_ethn$total_raceE)*100)

#Filter for the highest
head(race_ethn) #I like to do this after creating/changing variables to make sure the data changed properly
class(race_ethn) #check class of data to make sure the functions you use are compatible
## [1] "sf"         "data.frame"
print(race_ethn$BlAfm_pop) #allows you to check for null and void values that have to be removed
##   [1] 90.4770287 93.1399632 29.1610588 98.9931350  3.6692369 49.6315561
##   [7] 95.7079971 89.0890052 95.9811828 45.5273698  9.5333668 89.5314788
##  [13] 10.7583037 51.1227545 90.4672507 45.2655889 87.0240296 91.9772257
##  [19] 51.0378510 80.9184481 64.6026832 98.1004902 12.7139364 32.9875519
##  [25] 93.6466942 41.2393162 10.1311085 75.3156945 76.8650461 19.0839695
##  [31] 31.5710723 86.7805932 13.0136986 93.1431201 83.2222222 96.5502183
##  [37]  4.4786564 78.9679437 78.7374602 93.0962343 93.1522354 23.7938873
##  [43] 96.4501815 51.4950166 19.8521916 71.9130896 91.8160726  8.5267407
##  [49]  5.7363486 52.5944907 95.3488372 89.5325872 78.2198247 21.5792908
##  [55] 98.9323843 88.7739164 42.7395456 61.1356932  8.3503889 90.1952278
##  [61] 22.0661531 60.3902954  5.2694254 98.0278980 98.6961451 79.6246649
##  [67] 99.3177388 90.9841707 71.6184519 57.2948688  2.8831563 94.7768281
##  [73] 48.4395319 95.1206140 26.4715140 94.3141153 90.3257329  8.2208293
##  [79] 95.5364807 44.0191388 13.2933105 88.4082002 49.9858317 67.3726388
##  [85] 90.7333712 54.1639558 48.2620602 46.3279557  5.8927311 92.2963834
##  [91] 95.0740741 90.9634551 33.4349593 93.6949153 67.3724213 44.5533358
##  [97] 95.3249182 88.5801129 93.9003436 63.8866397 92.8211213 44.0864023
## [103] 95.3219083  5.0359712 95.6563707 93.7583001 82.0916905 16.5825523
## [109] 91.3278547 60.7506951 83.3547283 45.6279809 68.1635389 11.2803702
## [115] 97.4984365 24.8827078 97.4115334 71.0169815 66.2088975 60.5324373
## [121]  1.6386555 89.5477005 95.6026320 10.2238240  7.1732955 93.1277056
## [127]  8.9621140 98.5031917 76.8054823 34.1399032 33.8365897 93.6357481
## [133] 10.5914099  1.8975904 41.8621180 93.3884298        NaN 93.9987366
## [139] 39.0969900 83.6597938 79.4102694  6.4757161 92.0663650 14.2106327
## [145] 46.2032086 93.1628136 73.7564009 86.1308117 84.6884900 87.3330263
## [151] 17.3333333 42.7609428 59.0760425 95.5264969 81.3241724 88.2949309
## [157] 74.3362832 85.1226994 90.8235294 89.9005525 29.6772916 95.8609272
## [163] 80.7094266  6.7545305  4.8434086 84.3799954 52.1917808 48.0504587
## [169] 66.6810904 95.7838480 90.8902692 26.6174346 12.5667152  5.0509165
## [175] 93.2513915 77.5761974 66.2133891 95.7312806 18.6929023 94.6111451
## [181] 10.5979870  7.6045627 89.9301513 96.8794326 88.8644574  0.5145167
## [187] 76.9736842 92.7542754 96.2095875 65.9347553 82.6503459  2.4894068
## [193] 92.7789934 81.6793893 97.5630430 95.9215281 81.6457632  2.3184453
## [199] 92.8080050 28.7158984
race_ethnOMIT <- race_ethn %>% filter(!is.nan(BlAfm_pop))
 #creates new variable omitting null values

print(race_ethnOMIT$BlAfm_pop) #double check to make sure it worked 
##   [1] 90.4770287 93.1399632 29.1610588 98.9931350  3.6692369 49.6315561
##   [7] 95.7079971 89.0890052 95.9811828 45.5273698  9.5333668 89.5314788
##  [13] 10.7583037 51.1227545 90.4672507 45.2655889 87.0240296 91.9772257
##  [19] 51.0378510 80.9184481 64.6026832 98.1004902 12.7139364 32.9875519
##  [25] 93.6466942 41.2393162 10.1311085 75.3156945 76.8650461 19.0839695
##  [31] 31.5710723 86.7805932 13.0136986 93.1431201 83.2222222 96.5502183
##  [37]  4.4786564 78.9679437 78.7374602 93.0962343 93.1522354 23.7938873
##  [43] 96.4501815 51.4950166 19.8521916 71.9130896 91.8160726  8.5267407
##  [49]  5.7363486 52.5944907 95.3488372 89.5325872 78.2198247 21.5792908
##  [55] 98.9323843 88.7739164 42.7395456 61.1356932  8.3503889 90.1952278
##  [61] 22.0661531 60.3902954  5.2694254 98.0278980 98.6961451 79.6246649
##  [67] 99.3177388 90.9841707 71.6184519 57.2948688  2.8831563 94.7768281
##  [73] 48.4395319 95.1206140 26.4715140 94.3141153 90.3257329  8.2208293
##  [79] 95.5364807 44.0191388 13.2933105 88.4082002 49.9858317 67.3726388
##  [85] 90.7333712 54.1639558 48.2620602 46.3279557  5.8927311 92.2963834
##  [91] 95.0740741 90.9634551 33.4349593 93.6949153 67.3724213 44.5533358
##  [97] 95.3249182 88.5801129 93.9003436 63.8866397 92.8211213 44.0864023
## [103] 95.3219083  5.0359712 95.6563707 93.7583001 82.0916905 16.5825523
## [109] 91.3278547 60.7506951 83.3547283 45.6279809 68.1635389 11.2803702
## [115] 97.4984365 24.8827078 97.4115334 71.0169815 66.2088975 60.5324373
## [121]  1.6386555 89.5477005 95.6026320 10.2238240  7.1732955 93.1277056
## [127]  8.9621140 98.5031917 76.8054823 34.1399032 33.8365897 93.6357481
## [133] 10.5914099  1.8975904 41.8621180 93.3884298 93.9987366 39.0969900
## [139] 83.6597938 79.4102694  6.4757161 92.0663650 14.2106327 46.2032086
## [145] 93.1628136 73.7564009 86.1308117 84.6884900 87.3330263 17.3333333
## [151] 42.7609428 59.0760425 95.5264969 81.3241724 88.2949309 74.3362832
## [157] 85.1226994 90.8235294 89.9005525 29.6772916 95.8609272 80.7094266
## [163]  6.7545305  4.8434086 84.3799954 52.1917808 48.0504587 66.6810904
## [169] 95.7838480 90.8902692 26.6174346 12.5667152  5.0509165 93.2513915
## [175] 77.5761974 66.2133891 95.7312806 18.6929023 94.6111451 10.5979870
## [181]  7.6045627 89.9301513 96.8794326 88.8644574  0.5145167 76.9736842
## [187] 92.7542754 96.2095875 65.9347553 82.6503459  2.4894068 92.7789934
## [193] 81.6793893 97.5630430 95.9215281 81.6457632  2.3184453 92.8080050
## [199] 28.7158984
filter(race_ethnOMIT, BlAfm_pop > 99.00) # shows the highest percent census tracts with data so you can select the one with highest percent
#Finding the census tract with highest median household income
print(med_income$incomeE) #allows you to gauge the range of income and select high range 
##   [1]  41538  17371  38519  15195 108026  37451  27109  62586  52334  67895
##  [11]  67500  50354  64794  41184  43257  50761  13841  37917  76591  32772
##  [21]  17000  48750  89000  51765  29375  74578  84489     NA  19531  87500
##  [31]  63509  64028  56490  13289  36985  33156 106250  22464  56670  31440
##  [41]  20884  47321  22881  32992  49603  64922  49005 107391 133152  51089
##  [51]  30972  30000  43327 195156  36577  59524  69034  20820  56036  25190
##  [61]  65365  83160 109618  41833  31625  27778  44830  36700  59219  75197
##  [71] 128900  36824  43878  30809  55796  40039  53194  40281  32273  72132
##  [81]  40795  41280  45909  44229  31250  77977  39748  56750 122765  45855
##  [91]  26817  35313  43417  27174  38445  63421  50167  26581  36375  72216
## [101]  56530  55221  32747 103911  27623  26350  58441 104375  39432  17925
## [111]  38194  71500  49722  57639  39208  49970  44015  49327  71847  66845
## [121] 142368  39233  50798  42674 104250  31292 165443  37870  51938  72386
## [131]  41307  32902  80129 142328  31285  14915     NA  53333  55750  34873
## [141]  40996 133333  38934  41698  77792  31361  57973  44423  25398  28550
## [151]  65304  57014  59886  23295  24457  48922  51750  44390  40147  53120
## [161]  64016  39851  57991 125417 132281  31885  50375  30878  39159  42547
## [171]  34449  69016  45781 154313  16750  71189  55285  16080  92885  43306
## [181]  59883  72404  47566  30714  50686 110982  32276  42642  22571  31088
## [191]  44759 128227  20717  48175  36446  23417  46493  83050  49531  37395
filter(med_income, incomeE > 150000) #continue editing the income value to be greater than until list is filtered to highest income censust tracts
MedInc_OMIT <- med_income %>% filter(!is.na(incomeE))

print(MedInc_OMIT)
## Simple feature collection with 198 features and 4 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -76.71152 ymin: 39.19721 xmax: -76.52945 ymax: 39.3722
## geographic CRS: NAD83
## First 10 features:
##          GEOID                                           NAME incomeE incomeM
## 1  24510260202 Census Tract 2602.02, Baltimore city, Maryland   41538    7943
## 2  24510150100    Census Tract 1501, Baltimore city, Maryland   17371   10440
## 3  24510250303 Census Tract 2503.03, Baltimore city, Maryland   38519   11718
## 4  24510180100    Census Tract 1801, Baltimore city, Maryland   15195    5979
## 5  24510020300     Census Tract 203, Baltimore city, Maryland  108026   22478
## 6  24510250402 Census Tract 2504.02, Baltimore city, Maryland   37451   12822
## 7  24510150600    Census Tract 1506, Baltimore city, Maryland   27109   14923
## 8  24510260102 Census Tract 2601.02, Baltimore city, Maryland   62586    7913
## 9  24510151100    Census Tract 1511, Baltimore city, Maryland   52334    8557
## 10 24510270703 Census Tract 2707.03, Baltimore city, Maryland   67895   13694
##                          geometry
## 1  MULTIPOLYGON (((-76.56596 3...
## 2  MULTIPOLYGON (((-76.64501 3...
## 3  MULTIPOLYGON (((-76.655 39....
## 4  MULTIPOLYGON (((-76.63413 3...
## 5  MULTIPOLYGON (((-76.60051 3...
## 6  MULTIPOLYGON (((-76.60817 3...
## 7  MULTIPOLYGON (((-76.68057 3...
## 8  MULTIPOLYGON (((-76.55166 3...
## 9  MULTIPOLYGON (((-76.67867 3...
## 10 MULTIPOLYGON (((-76.56192 3...

2. Reproject this data to Web Mercator.

BlAfm_Web <- st_transform(race_ethnOMIT, 3857) 

medinc_web <- st_transform(MedInc_OMIT, 3857)

st_crs(BlAfm_Web) #check to see if it worked 
## Coordinate Reference System:
##   User input: EPSG:3857 
##   wkt:
## PROJCRS["WGS 84 / Pseudo-Mercator",
##     BASEGEOGCRS["WGS 84",
##         DATUM["World Geodetic System 1984",
##             ELLIPSOID["WGS 84",6378137,298.257223563,
##                 LENGTHUNIT["metre",1]]],
##         PRIMEM["Greenwich",0,
##             ANGLEUNIT["degree",0.0174532925199433]],
##         ID["EPSG",4326]],
##     CONVERSION["Popular Visualisation Pseudo-Mercator",
##         METHOD["Popular Visualisation Pseudo Mercator",
##             ID["EPSG",1024]],
##         PARAMETER["Latitude of natural origin",0,
##             ANGLEUNIT["degree",0.0174532925199433],
##             ID["EPSG",8801]],
##         PARAMETER["Longitude of natural origin",0,
##             ANGLEUNIT["degree",0.0174532925199433],
##             ID["EPSG",8802]],
##         PARAMETER["False easting",0,
##             LENGTHUNIT["metre",1],
##             ID["EPSG",8806]],
##         PARAMETER["False northing",0,
##             LENGTHUNIT["metre",1],
##             ID["EPSG",8807]]],
##     CS[Cartesian,2],
##         AXIS["easting (X)",east,
##             ORDER[1],
##             LENGTHUNIT["metre",1]],
##         AXIS["northing (Y)",north,
##             ORDER[2],
##             LENGTHUNIT["metre",1]],
##     USAGE[
##         SCOPE["unknown"],
##         AREA["World - 85°S to 85°N"],
##         BBOX[-85.06,-180,85.06,180]],
##     ID["EPSG",3857]]
st_crs(medinc_web)
## Coordinate Reference System:
##   User input: EPSG:3857 
##   wkt:
## PROJCRS["WGS 84 / Pseudo-Mercator",
##     BASEGEOGCRS["WGS 84",
##         DATUM["World Geodetic System 1984",
##             ELLIPSOID["WGS 84",6378137,298.257223563,
##                 LENGTHUNIT["metre",1]]],
##         PRIMEM["Greenwich",0,
##             ANGLEUNIT["degree",0.0174532925199433]],
##         ID["EPSG",4326]],
##     CONVERSION["Popular Visualisation Pseudo-Mercator",
##         METHOD["Popular Visualisation Pseudo Mercator",
##             ID["EPSG",1024]],
##         PARAMETER["Latitude of natural origin",0,
##             ANGLEUNIT["degree",0.0174532925199433],
##             ID["EPSG",8801]],
##         PARAMETER["Longitude of natural origin",0,
##             ANGLEUNIT["degree",0.0174532925199433],
##             ID["EPSG",8802]],
##         PARAMETER["False easting",0,
##             LENGTHUNIT["metre",1],
##             ID["EPSG",8806]],
##         PARAMETER["False northing",0,
##             LENGTHUNIT["metre",1],
##             ID["EPSG",8807]]],
##     CS[Cartesian,2],
##         AXIS["easting (X)",east,
##             ORDER[1],
##             LENGTHUNIT["metre",1]],
##         AXIS["northing (Y)",north,
##             ORDER[2],
##             LENGTHUNIT["metre",1]],
##     USAGE[
##         SCOPE["unknown"],
##         AREA["World - 85°S to 85°N"],
##         BBOX[-85.06,-180,85.06,180]],
##     ID["EPSG",3857]]

3. Create two plots. In the first plot highlight the tract with the highest concentration of your selected race/eth. In the second plot highlight the tract with the highest median household income.

BlAfm_Web %>%
  ggplot() +
  geom_sf(aes(fill = BlAfm_pop > 99.00 )) +
  scale_fill_discrete(name = "Legend", labels = c("Tracts < 99%", "Census Tract 2007.02")) +
  ggtitle("Highest % Population of Non Hispanic/Latine 
Black or African American Population Census Tract") +
  theme(plot.title = element_text(hjust = 1), plot.subtitle = element_text(hjust =  0.5)) +
  bi_theme(base_size = 10)

medinc_web %>%
  ggplot() +
  geom_sf(aes(fill = incomeE > 190000 )) +
  scale_fill_discrete(name = "Legend", labels = c("Tracts w/ Incomes < $190,000", "Census Tract 2711.02")) +
  ggtitle("Highest Median Household Income Census Tract") +
  theme(plot.title = element_text(hjust = 1), plot.subtitle = element_text(hjust =  0.5)) +
  bi_theme(base_size = 10)

4. Create a third column using the bi_class function from the tutorial. (2 points)

#Join two datasets together based on GEOID column
ethinc_join <- st_join(medinc_web, BlAfm_Web, by = c("GEOID" = "GEOID"), copy = TRUE) 

print(ethinc_join) #I use this to check and make sure it worked 
## Simple feature collection with 1402 features and 13 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -8539487 ymin: 4749959 xmax: -8519220 ymax: 4775128
## projected CRS:  WGS 84 / Pseudo-Mercator
## First 10 features:
##         GEOID.x                                         NAME.x incomeE incomeM
## 1   24510260202 Census Tract 2602.02, Baltimore city, Maryland   41538    7943
## 1.1 24510260202 Census Tract 2602.02, Baltimore city, Maryland   41538    7943
## 1.2 24510260202 Census Tract 2602.02, Baltimore city, Maryland   41538    7943
## 1.3 24510260202 Census Tract 2602.02, Baltimore city, Maryland   41538    7943
## 1.4 24510260202 Census Tract 2602.02, Baltimore city, Maryland   41538    7943
## 1.5 24510260202 Census Tract 2602.02, Baltimore city, Maryland   41538    7943
## 1.6 24510260202 Census Tract 2602.02, Baltimore city, Maryland   41538    7943
## 1.7 24510260202 Census Tract 2602.02, Baltimore city, Maryland   41538    7943
## 1.8 24510260202 Census Tract 2602.02, Baltimore city, Maryland   41538    7943
## 2   24510150100    Census Tract 1501, Baltimore city, Maryland   17371   10440
##         GEOID.y                                         NAME.y total_raceE
## 1   24510260202 Census Tract 2602.02, Baltimore city, Maryland        5681
## 1.1 24510270101 Census Tract 2701.01, Baltimore city, Maryland        1638
## 1.2 24510260303 Census Tract 2603.03, Baltimore city, Maryland         969
## 1.3 24510270102 Census Tract 2701.02, Baltimore city, Maryland        3774
## 1.4 24510260301 Census Tract 2603.01, Baltimore city, Maryland        4741
## 1.5 24510260203 Census Tract 2602.03, Baltimore city, Maryland        3070
## 1.6 24510260302 Census Tract 2603.02, Baltimore city, Maryland        6634
## 1.7 24510080101  Census Tract 801.01, Baltimore city, Maryland        3869
## 1.8 24510260201 Census Tract 2602.01, Baltimore city, Maryland        5901
## 2   24510150100    Census Tract 1501, Baltimore city, Maryland        2172
##     total_raceM not_horlE not_horlM BlAfmE BlAfmM BlAfm_pop
## 1           637      5493       610   5140    627  90.47703
## 1.1         224      1600       225    836    209  51.03785
## 1.2         144       903       135    626    166  64.60268
## 1.3         547      3699       542   2714    570  71.91309
## 1.4         718      4727       721   4353    719  91.81607
## 1.5         272      3063       273   2773    324  90.32573
## 1.6         918      6470       922   5865    977  88.40820
## 1.7         447      3773       441   2342    455  60.53244
## 1.8         621      5839       613   4686    738  79.41027
## 2           383      2172       383   2023    377  93.13996
##                           geometry
## 1   MULTIPOLYGON (((-8523284 47...
## 1.1 MULTIPOLYGON (((-8523284 47...
## 1.2 MULTIPOLYGON (((-8523284 47...
## 1.3 MULTIPOLYGON (((-8523284 47...
## 1.4 MULTIPOLYGON (((-8523284 47...
## 1.5 MULTIPOLYGON (((-8523284 47...
## 1.6 MULTIPOLYGON (((-8523284 47...
## 1.7 MULTIPOLYGON (((-8523284 47...
## 1.8 MULTIPOLYGON (((-8523284 47...
## 2   MULTIPOLYGON (((-8532083 47...
#Create a new variable of the joined data and create biclass column
join_data <- bi_class(ethinc_join, x = BlAfm_pop, y = incomeE, style = "equal", dim = 3) 

#Check to see if biclass column was formed
print(join_data) 
## Simple feature collection with 1402 features and 14 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -8539487 ymin: 4749959 xmax: -8519220 ymax: 4775128
## projected CRS:  WGS 84 / Pseudo-Mercator
## First 10 features:
##         GEOID.x                                         NAME.x incomeE incomeM
## 1   24510260202 Census Tract 2602.02, Baltimore city, Maryland   41538    7943
## 1.1 24510260202 Census Tract 2602.02, Baltimore city, Maryland   41538    7943
## 1.2 24510260202 Census Tract 2602.02, Baltimore city, Maryland   41538    7943
## 1.3 24510260202 Census Tract 2602.02, Baltimore city, Maryland   41538    7943
## 1.4 24510260202 Census Tract 2602.02, Baltimore city, Maryland   41538    7943
## 1.5 24510260202 Census Tract 2602.02, Baltimore city, Maryland   41538    7943
## 1.6 24510260202 Census Tract 2602.02, Baltimore city, Maryland   41538    7943
## 1.7 24510260202 Census Tract 2602.02, Baltimore city, Maryland   41538    7943
## 1.8 24510260202 Census Tract 2602.02, Baltimore city, Maryland   41538    7943
## 2   24510150100    Census Tract 1501, Baltimore city, Maryland   17371   10440
##         GEOID.y                                         NAME.y total_raceE
## 1   24510260202 Census Tract 2602.02, Baltimore city, Maryland        5681
## 1.1 24510270101 Census Tract 2701.01, Baltimore city, Maryland        1638
## 1.2 24510260303 Census Tract 2603.03, Baltimore city, Maryland         969
## 1.3 24510270102 Census Tract 2701.02, Baltimore city, Maryland        3774
## 1.4 24510260301 Census Tract 2603.01, Baltimore city, Maryland        4741
## 1.5 24510260203 Census Tract 2602.03, Baltimore city, Maryland        3070
## 1.6 24510260302 Census Tract 2603.02, Baltimore city, Maryland        6634
## 1.7 24510080101  Census Tract 801.01, Baltimore city, Maryland        3869
## 1.8 24510260201 Census Tract 2602.01, Baltimore city, Maryland        5901
## 2   24510150100    Census Tract 1501, Baltimore city, Maryland        2172
##     total_raceM not_horlE not_horlM BlAfmE BlAfmM BlAfm_pop bi_class
## 1           637      5493       610   5140    627  90.47703      3-1
## 1.1         224      1600       225    836    209  51.03785      2-1
## 1.2         144       903       135    626    166  64.60268      2-1
## 1.3         547      3699       542   2714    570  71.91309      3-1
## 1.4         718      4727       721   4353    719  91.81607      3-1
## 1.5         272      3063       273   2773    324  90.32573      3-1
## 1.6         918      6470       922   5865    977  88.40820      3-1
## 1.7         447      3773       441   2342    455  60.53244      2-1
## 1.8         621      5839       613   4686    738  79.41027      3-1
## 2           383      2172       383   2023    377  93.13996      3-1
##                           geometry
## 1   MULTIPOLYGON (((-8523284 47...
## 1.1 MULTIPOLYGON (((-8523284 47...
## 1.2 MULTIPOLYGON (((-8523284 47...
## 1.3 MULTIPOLYGON (((-8523284 47...
## 1.4 MULTIPOLYGON (((-8523284 47...
## 1.5 MULTIPOLYGON (((-8523284 47...
## 1.6 MULTIPOLYGON (((-8523284 47...
## 1.7 MULTIPOLYGON (((-8523284 47...
## 1.8 MULTIPOLYGON (((-8523284 47...
## 2   MULTIPOLYGON (((-8532083 47...

5. Create a bivariate map with your data.

library(ggplot2)

bivariate_map <- ggplot(join_data) +
  geom_sf(mapping = aes(fill = bi_class), color = "white", size = 0.1, show.legend = FALSE) +
  bi_scale_fill(pal = "GrPink", dim = 3) +
  ggtitle( "Race & Income in Baltimore City, MD",
    subtitle = "Black & African American Population")  +
  theme(plot.title = element_text(hjust = 0.5), plot.subtitle = element_text(hjust =  0.5)) +
  bi_theme(base_size = 13)

ggplot(join_data) +
  geom_sf(mapping = aes(fill = bi_class), color = "white", size = 0.1, show.legend = FALSE) +
  bi_scale_fill(pal = "GrPink", dim = 3) +
  ggtitle( "Race & Income in Baltimore City, MD",
    subtitle = "Black & African American Population")  +
  theme(plot.title = element_text(hjust = 0.5), plot.subtitle = element_text(hjust =  0.5)) +
  bi_theme(base_size = 13)

6. Use the cowplot package and ggdraw, like in the tutorial to add a legend

legend <- bi_legend(pal = "GrPink",
                    dim = 3,
                    xlab = "Higher % Black & African American",
                    ylab = "Higher Income",
                    size = 8)

finalplot <- ggdraw()+
  draw_plot(bivariate_map, 0, 0, 1, 1)+
  draw_plot(legend, 0.001, 0.001, 0.35, 0.35)

ggdraw(finalplot) 

7. Rinse and repeat for another county of your choosing, using a different color scheme.

#Download data 
race_ethnPG <- get_acs(geography = "tract", 
     variables = c("total_race" = "B03002_001", #Total Population data
                 "not_horl" = "B03002_002", #Not Hispanic or Latine
                 "BlAfm" = "B03002_004" #Black or African American alone
                   ),
     year = 2019,
     survey = "acs5",
     state = c(24), #Maryland
     county = c(033), #Baltimore City and PG County
     geometry = TRUE, # download the shapefile with the data
     output = "wide") # need this
## Getting data from the 2015-2019 5-year ACS
med_incomePG <- get_acs(geography = "tract",
                      variables = c("income" = "B19013_001" #median household income
                      ),
                      year = 2019,
                      survey = "acs5",
                      state = c(24),
                      county = c(033),
                      geometry = TRUE,
                      output = "wide")
## Getting data from the 2015-2019 5-year ACS

Census Tract with Highest Percentage: 24033803525 Census Tract 8035.25, Prince George’s County, Maryland

Percentage: 97.38%

Highest Median Household Income: 24033800518 Census Tract 8005.18, Prince George’s County, Maryland

Highest Median Income: $185625

#Census Tract with highest % of Black/African American population
race_ethnPG$BlAfm_pop <- ((race_ethnPG$BlAfmE / race_ethnPG$total_raceE)*100)

#Filter for the highest
head(race_ethnPG) #I like to do this after creating/changing variables to make sure the data changed properly
class(race_ethnPG) #check class of data to make sure the functions you use are compatible
## [1] "sf"         "data.frame"
print(race_ethnPG$BlAfm_pop) #allows you to check for null and void values that have to be removed
##   [1] 33.670653 30.119396 92.658680 86.375955 81.605839 74.819919 76.695174
##   [8] 68.969046 83.715442 32.439678 17.968606 47.762600 11.343048 85.674677
##  [15] 81.396339 87.880143 73.372135 73.230029 93.209682 41.370968 93.031927
##  [22] 22.212518 89.295446 32.207792 89.781958 65.110489 25.381306 81.369048
##  [29] 59.006550 55.183287 91.062870 18.467723 91.664433 39.995202 85.002329
##  [36] 66.465257 55.471184 29.667774 57.013091 90.777339 81.668357 87.528205
##  [43] 16.343596 79.783784 77.161557 89.763276 54.071661 85.431948 56.453362
##  [50] 91.999173 46.621301 44.458281 54.614045 69.979438 25.383492 75.311721
##  [57] 14.447770 56.217054 65.530126 51.242076 54.469411 28.433510 60.539579
##  [64] 81.923077 58.353365 22.087830 68.053357 78.533706 22.189554 90.197824
##  [71] 10.357616 91.248391 33.533569 87.838018 62.794521 69.103448 63.916242
##  [78] 47.933697 79.546681 70.374556 67.509482 91.639715 60.996441 40.051184
##  [85] 33.802450 94.635532 78.390214 71.517615 92.351939 41.486519 21.821381
##  [92] 90.626131 91.337931 53.786067 82.011385 66.615444 76.484987 80.486425
##  [99] 66.977015 41.436697 74.385349 86.324418 43.163694 39.312321 11.216588
## [106] 24.519373 63.340432 61.499105 73.109088 74.779434 68.847530 53.804863
## [113] 60.421082 86.246418 49.400480 88.193103 73.325554 85.633935  5.402542
## [120] 85.776585 93.245318 44.654513 68.735928 87.361496 85.731216 44.617134
## [127] 47.095275 81.654739 24.979608 14.972394 67.868474 59.797823 24.971407
## [134] 81.479242 26.932783 87.692066 28.078709 20.834944 78.455598 59.347826
## [141] 81.269270 17.078410 64.674571 73.582144 85.128098 78.524788 73.676739
## [148] 77.053584 33.730632 56.794682 32.209469 77.763636 38.774341 95.787458
## [155] 87.046632 73.119505 90.859973 61.135448 85.510836 15.221331 41.964481
## [162] 31.402214 39.258635 86.901486 91.598845 79.462422 97.383341 90.719967
## [169] 24.703500 72.958556 79.338643 64.523753 80.127264 85.671769 78.480021
## [176] 64.979654  3.531180 77.978265 27.902622 58.798405 60.025031 94.844517
## [183] 75.914565 73.085622 18.128995 43.441938 38.006891 76.648538 82.732095
## [190] 63.670500 82.977425 76.095977 91.223242 37.504919 89.744181 50.375177
## [197] 23.448832 56.832298 30.185005 48.637490 83.585125 89.680000 88.753936
## [204] 25.643516 54.525140 63.141684 66.038319 86.656830 48.450890 80.838323
## [211] 79.817444 47.437137 84.183411 72.222222 60.108939 85.595628 83.949136
## [218] 79.254850
filter(race_ethnPG, BlAfm_pop > 95.00) # shows the highest percent census tracts with data so you can select the one with highest percent
#Finding the census tract with highest median household income
print(med_incomePG$incomeE) #allows you to gauge the range of income and select high range 
##   [1]  75850  70179  61233  48257  90246  54784 105217 124444  82163  60379
##  [11]  76109  62557 101058  73884 106042  43661  70822 126413  78103  71750
##  [21]  58764  46023  64257  82500  80304  60332 106738  56220  77917  50184
##  [31]  57188  75129 126797  77112  77917  98315 105114  83375  88007  57072
##  [41]  64283 127537  94420 101576  56406  97688  88060  95326 103051  69659
##  [51] 124451  65556  92083  65625  94079 162688  35991 136331 141949 125977
##  [61]  86797  79536  65652  77708  71687 102340  89688  65833  59683 112917
##  [71] 130250  54844 101525 149350  59635  70750 105754  59563  85568 130202
##  [81] 116136  96065  51806  60882  77000 110265  91023  75862  84962  55586
##  [91] 102045  68021  80723  55891 121029  82273  93952 101761  53384  58534
## [101] 120938  58702  98553 107500 109010  39653 104420  78750  60984 106929
## [111] 136716  69205 131173 108152  69146 104453 117336  73162  68481 140708
## [121]  67031  88086  72413  57989  73169 129500  51887 156250 106000  75969
## [131] 138672  60105 121136  90028  51537 162536  86250  80452  54096 102057
## [141] 185625 134740  82000  76595 110893 135938 134167 139427  65250  63292
## [151]  92031  40697  56583  54612  53231  54194  70278  74315  80750  64323
## [161]  50346  77674  73750  55433  65484  49784  73672  59792  88065  77022
## [171] 139180 118464 103964  59018 134258  77197  58667  96410 124478  51159
## [181] 115000  68787  90978  47295  63625  66724 126507  62692 102882 127614
## [191] 114451 131739  47194  60500  83269  97284 113913  74375 125172  86495
## [201] 117000  83750  55657  85422  81964  65469 105327 121250  85543 100855
## [211]  81165  73375  65972  70904  76417  80278  63850 132813
filter(med_incomePG, incomeE > 185000) #continue editing the income value to be greater than until list is filtered to highest income census tracts

Reproject to Web Mercator

PGRace_Web <- st_transform(race_ethnPG, 3857) 

PGmedinc_Web <- st_transform(med_incomePG, 3857)

st_crs(PGRace_Web) #check to see if it worked 
## Coordinate Reference System:
##   User input: EPSG:3857 
##   wkt:
## PROJCRS["WGS 84 / Pseudo-Mercator",
##     BASEGEOGCRS["WGS 84",
##         DATUM["World Geodetic System 1984",
##             ELLIPSOID["WGS 84",6378137,298.257223563,
##                 LENGTHUNIT["metre",1]]],
##         PRIMEM["Greenwich",0,
##             ANGLEUNIT["degree",0.0174532925199433]],
##         ID["EPSG",4326]],
##     CONVERSION["Popular Visualisation Pseudo-Mercator",
##         METHOD["Popular Visualisation Pseudo Mercator",
##             ID["EPSG",1024]],
##         PARAMETER["Latitude of natural origin",0,
##             ANGLEUNIT["degree",0.0174532925199433],
##             ID["EPSG",8801]],
##         PARAMETER["Longitude of natural origin",0,
##             ANGLEUNIT["degree",0.0174532925199433],
##             ID["EPSG",8802]],
##         PARAMETER["False easting",0,
##             LENGTHUNIT["metre",1],
##             ID["EPSG",8806]],
##         PARAMETER["False northing",0,
##             LENGTHUNIT["metre",1],
##             ID["EPSG",8807]]],
##     CS[Cartesian,2],
##         AXIS["easting (X)",east,
##             ORDER[1],
##             LENGTHUNIT["metre",1]],
##         AXIS["northing (Y)",north,
##             ORDER[2],
##             LENGTHUNIT["metre",1]],
##     USAGE[
##         SCOPE["unknown"],
##         AREA["World - 85°S to 85°N"],
##         BBOX[-85.06,-180,85.06,180]],
##     ID["EPSG",3857]]
st_crs(PGmedinc_Web)
## Coordinate Reference System:
##   User input: EPSG:3857 
##   wkt:
## PROJCRS["WGS 84 / Pseudo-Mercator",
##     BASEGEOGCRS["WGS 84",
##         DATUM["World Geodetic System 1984",
##             ELLIPSOID["WGS 84",6378137,298.257223563,
##                 LENGTHUNIT["metre",1]]],
##         PRIMEM["Greenwich",0,
##             ANGLEUNIT["degree",0.0174532925199433]],
##         ID["EPSG",4326]],
##     CONVERSION["Popular Visualisation Pseudo-Mercator",
##         METHOD["Popular Visualisation Pseudo Mercator",
##             ID["EPSG",1024]],
##         PARAMETER["Latitude of natural origin",0,
##             ANGLEUNIT["degree",0.0174532925199433],
##             ID["EPSG",8801]],
##         PARAMETER["Longitude of natural origin",0,
##             ANGLEUNIT["degree",0.0174532925199433],
##             ID["EPSG",8802]],
##         PARAMETER["False easting",0,
##             LENGTHUNIT["metre",1],
##             ID["EPSG",8806]],
##         PARAMETER["False northing",0,
##             LENGTHUNIT["metre",1],
##             ID["EPSG",8807]]],
##     CS[Cartesian,2],
##         AXIS["easting (X)",east,
##             ORDER[1],
##             LENGTHUNIT["metre",1]],
##         AXIS["northing (Y)",north,
##             ORDER[2],
##             LENGTHUNIT["metre",1]],
##     USAGE[
##         SCOPE["unknown"],
##         AREA["World - 85°S to 85°N"],
##         BBOX[-85.06,-180,85.06,180]],
##     ID["EPSG",3857]]

Create plots hihglighting the census tracts found

PGRace_Web %>%
  ggplot() +
  geom_sf(aes(fill = BlAfm_pop > 97.00)) +
  scale_fill_discrete(name = "Legend", labels = c("Tracts < 97%", "Census Tract 8035.25")) +
  ggtitle("Highest % Population of Non Hispanic/Latine 
Black or African American Population Census Tract", subtitle = "Prince George's County, MD") +
  theme(plot.title = element_text(hjust = 1), plot.subtitle = element_text(hjust =  0.5)) +
  bi_theme(base_size = 10)

PGmedinc_Web %>%
  ggplot() +
  geom_sf(aes(fill = incomeE > 182000)) +
  scale_fill_discrete(name = "Legend", labels = c("Tracts w/ Incomes < $185,000", "Census Tract 8005.18")) +
  ggtitle("Highest Median Household Income Census Tract", subtitle = "Prince George's County, MD") +
  theme(plot.title = element_text(hjust = 1), plot.subtitle = element_text(hjust =  0.5)) +
  bi_theme(base_size = 10)

Create bivariate column using bi_class

#Join two datasets together based on GEOID column
PGethinc_join <- st_join(PGmedinc_Web, PGRace_Web, by = c("GEOID" = "GEOID"), copy = TRUE) 

print(PGethinc_join) #I use this to check and make sure it worked 
## Simple feature collection with 1498 features and 13 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -8581204 ymin: 4655334 xmax: -8534811 ymax: 4740496
## projected CRS:  WGS 84 / Pseudo-Mercator
## First 10 features:
##         GEOID.x                                                 NAME.x incomeE
## 1   24033806602 Census Tract 8066.02, Prince George's County, Maryland   75850
## 1.1 24033806602 Census Tract 8066.02, Prince George's County, Maryland   75850
## 1.2 24033806602 Census Tract 8066.02, Prince George's County, Maryland   75850
## 1.3 24033806602 Census Tract 8066.02, Prince George's County, Maryland   75850
## 1.4 24033806602 Census Tract 8066.02, Prince George's County, Maryland   75850
## 1.5 24033806602 Census Tract 8066.02, Prince George's County, Maryland   75850
## 2   24033806708 Census Tract 8067.08, Prince George's County, Maryland   70179
## 2.1 24033806708 Census Tract 8067.08, Prince George's County, Maryland   70179
## 2.2 24033806708 Census Tract 8067.08, Prince George's County, Maryland   70179
## 2.3 24033806708 Census Tract 8067.08, Prince George's County, Maryland   70179
##     incomeM     GEOID.y                                                 NAME.y
## 1      9498 24033806602 Census Tract 8066.02, Prince George's County, Maryland
## 1.1    9498 24033803803 Census Tract 8038.03, Prince George's County, Maryland
## 1.2    9498 24033807102 Census Tract 8071.02, Prince George's County, Maryland
## 1.3    9498 24033803605 Census Tract 8036.05, Prince George's County, Maryland
## 1.4    9498 24033806706 Census Tract 8067.06, Prince George's County, Maryland
## 1.5    9498 24033806601 Census Tract 8066.01, Prince George's County, Maryland
## 2     15200 24033806708 Census Tract 8067.08, Prince George's County, Maryland
## 2.1   15200 24033806710 Census Tract 8067.10, Prince George's County, Maryland
## 2.2   15200 24033807408 Census Tract 8074.08, Prince George's County, Maryland
## 2.3   15200 24033806706 Census Tract 8067.06, Prince George's County, Maryland
##     total_raceE total_raceM not_horlE not_horlM BlAfmE BlAfmM BlAfm_pop
## 1          5435         532      3308       435   1830    315  33.67065
## 1.1        6020         599      2163       456   1786    413  29.66777
## 1.2        2631         235      1976       236    430    107  16.34360
## 1.3        6897         583      4397       404   2977    399  43.16369
## 1.4        3167         408      2690       391   1704    346  53.80486
## 1.5        4359         515      1313       240   1174    220  26.93278
## 2          4439         436      4175       394   1337    398  30.11940
## 2.1        4992         408      4785       444   2913    506  58.35337
## 2.2        5660         515      4894       361   1898    376  33.53357
## 2.3        3167         408      2690       391   1704    346  53.80486
##                           geometry
## 1   MULTIPOLYGON (((-8562625 47...
## 1.1 MULTIPOLYGON (((-8562625 47...
## 1.2 MULTIPOLYGON (((-8562625 47...
## 1.3 MULTIPOLYGON (((-8562625 47...
## 1.4 MULTIPOLYGON (((-8562625 47...
## 1.5 MULTIPOLYGON (((-8562625 47...
## 2   MULTIPOLYGON (((-8559675 47...
## 2.1 MULTIPOLYGON (((-8559675 47...
## 2.2 MULTIPOLYGON (((-8559675 47...
## 2.3 MULTIPOLYGON (((-8559675 47...
#Create a new variable of the joined data and create biclass column
PGjoin_data <- bi_class(PGethinc_join, x = BlAfm_pop, y = incomeE, style = "equal", dim = 3) 

#Check to see if biclass column was formed
print(PGjoin_data) 
## Simple feature collection with 1498 features and 14 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -8581204 ymin: 4655334 xmax: -8534811 ymax: 4740496
## projected CRS:  WGS 84 / Pseudo-Mercator
## First 10 features:
##         GEOID.x                                                 NAME.x incomeE
## 1   24033806602 Census Tract 8066.02, Prince George's County, Maryland   75850
## 1.1 24033806602 Census Tract 8066.02, Prince George's County, Maryland   75850
## 1.2 24033806602 Census Tract 8066.02, Prince George's County, Maryland   75850
## 1.3 24033806602 Census Tract 8066.02, Prince George's County, Maryland   75850
## 1.4 24033806602 Census Tract 8066.02, Prince George's County, Maryland   75850
## 1.5 24033806602 Census Tract 8066.02, Prince George's County, Maryland   75850
## 2   24033806708 Census Tract 8067.08, Prince George's County, Maryland   70179
## 2.1 24033806708 Census Tract 8067.08, Prince George's County, Maryland   70179
## 2.2 24033806708 Census Tract 8067.08, Prince George's County, Maryland   70179
## 2.3 24033806708 Census Tract 8067.08, Prince George's County, Maryland   70179
##     incomeM     GEOID.y                                                 NAME.y
## 1      9498 24033806602 Census Tract 8066.02, Prince George's County, Maryland
## 1.1    9498 24033803803 Census Tract 8038.03, Prince George's County, Maryland
## 1.2    9498 24033807102 Census Tract 8071.02, Prince George's County, Maryland
## 1.3    9498 24033803605 Census Tract 8036.05, Prince George's County, Maryland
## 1.4    9498 24033806706 Census Tract 8067.06, Prince George's County, Maryland
## 1.5    9498 24033806601 Census Tract 8066.01, Prince George's County, Maryland
## 2     15200 24033806708 Census Tract 8067.08, Prince George's County, Maryland
## 2.1   15200 24033806710 Census Tract 8067.10, Prince George's County, Maryland
## 2.2   15200 24033807408 Census Tract 8074.08, Prince George's County, Maryland
## 2.3   15200 24033806706 Census Tract 8067.06, Prince George's County, Maryland
##     total_raceE total_raceM not_horlE not_horlM BlAfmE BlAfmM BlAfm_pop
## 1          5435         532      3308       435   1830    315  33.67065
## 1.1        6020         599      2163       456   1786    413  29.66777
## 1.2        2631         235      1976       236    430    107  16.34360
## 1.3        6897         583      4397       404   2977    399  43.16369
## 1.4        3167         408      2690       391   1704    346  53.80486
## 1.5        4359         515      1313       240   1174    220  26.93278
## 2          4439         436      4175       394   1337    398  30.11940
## 2.1        4992         408      4785       444   2913    506  58.35337
## 2.2        5660         515      4894       361   1898    376  33.53357
## 2.3        3167         408      2690       391   1704    346  53.80486
##     bi_class                       geometry
## 1        1-1 MULTIPOLYGON (((-8562625 47...
## 1.1      1-1 MULTIPOLYGON (((-8562625 47...
## 1.2      1-1 MULTIPOLYGON (((-8562625 47...
## 1.3      2-1 MULTIPOLYGON (((-8562625 47...
## 1.4      2-1 MULTIPOLYGON (((-8562625 47...
## 1.5      1-1 MULTIPOLYGON (((-8562625 47...
## 2        1-1 MULTIPOLYGON (((-8559675 47...
## 2.1      2-1 MULTIPOLYGON (((-8559675 47...
## 2.2      1-1 MULTIPOLYGON (((-8559675 47...
## 2.3      2-1 MULTIPOLYGON (((-8559675 47...

**Create bivariate map of PG County

PGbivariate_map <- ggplot(PGjoin_data) +
  geom_sf(mapping = aes(fill = bi_class), color = "white", size = 0.1, show.legend = FALSE) +
  bi_scale_fill(pal = "DkViolet", dim = 3) +
  ggtitle( "Race & Income in Prince George's County, MD",
    subtitle = "Black & African American Population")  +
  theme(plot.title = element_text(hjust = 0.5), plot.subtitle = element_text(hjust =  0.5)) +
  bi_theme(base_size = 13)

ggplot(PGjoin_data) +
  geom_sf(mapping = aes(fill = bi_class), color = "white", size = 0.1, show.legend = FALSE) +
  bi_scale_fill(pal = "DkViolet", dim = 3) +
  ggtitle( "Race & Income in Prince George's County, MD",
    subtitle = "Black & African American Population")  +
  theme(plot.title = element_text(hjust = 0.5), plot.subtitle = element_text(hjust =  0.5)) +
  bi_theme(base_size = 13)

Create legend and add it to final map

PGlegend <- bi_legend(pal = "DkViolet",
                    dim = 3,
                    xlab = "Higher % Black & African American",
                    ylab = "Higher Income",
                    size = 8)

PGfinalplot <- ggdraw()+
  draw_plot(PGbivariate_map, 0, 0, 1, 1)+
  draw_plot(PGlegend, 0.001, 0.001, 0.35, 0.35)

ggdraw(PGfinalplot) 

8. Write the bi_class output to a geojson file.

library(geojsonio)
## Warning: package 'geojsonio' was built under R version 4.0.5
## Registered S3 method overwritten by 'geojsonsf':
##   method        from   
##   print.geojson geojson
## 
## Attaching package: 'geojsonio'
## The following object is masked from 'package:base':
## 
##     pretty
geojson_write(PGjoin_data, file = "C:/Users/poppa/Documents/UMBC_School_Stuff/GES486/Lab8/PGjoin_data.geojson")
## Success! File is at C:/Users/poppa/Documents/UMBC_School_Stuff/GES486/Lab8/PGjoin_data.geojson
## <geojson-file>
##   Path:       C:/Users/poppa/Documents/UMBC_School_Stuff/GES486/Lab8/PGjoin_data.geojson
##   From class: geo_list

9. Now open your geojson output and create a QGIS map of your bivariate map. Put an image of that map here.

Bivariate Map PG

10. Use qgis2web and put a link here to your github site with the webmap of your bivariate map.

sarahqj9.github.io/Lab8/qgis2web_2021_04_29-16_09_48_190893/Lab8.html

Reflection I feel like I finally have a good grasp of how to utilize ggplot and ggplot2 to edit and create maps that are more designed than what I used to be able to do. This is the first all or mostly coding assignment I have felt confident in my ability to troubleshoot on my own, and have a good sense of how to figure out what I need to get the results I wanted. Being completely new to the coding world, I think I have been struggling with just understanding the language as a language, and not necessarily just following along with the tutorials or guides to get a specific result. I am beginning to understand better what the different components of code are made up of, and how to use operators, variables, and functions in ways that align with one another. It didn’t fully click for me previously that each package I use has different operators, and I would try to use operators that did not work or were not dependent on the packages that I was actually using. Overall feeling more confident in RStudio all around.